<?php
namespace app\weixin\controller;





use think\Db;
use think\Request;

class Index extends \think\Controller
{
    public function index()
    {  
     

        $newslist=Db::table('news')->order('ID desc')->limit(5)->select();
        $this->assign('newslist',$newslist);
        $where['state']=3;
        $prolist=Db::table('product_1')->where($where)->order('rand()')->limit(4)->select();
        $this->assign('prolist',$prolist);

          

         $ad=Db::table('ad')->where("type","index")->order('sort desc')->select();
         $this->assign('ad',$ad);


        return view();


    }


    public function indexsj(){

        $newslist=Db::table('news')->order('ID desc')->limit(5)->select();
        $this->assign('newslist',$newslist);
        $where['state']=3;
        $prolist=Db::table('product_1')->where($where)->order('rand()')->limit(4)->select();
        $this->assign('prolist',$prolist);


        $ad=Db::table('ad')->where("type","index")->order('sort desc')->select();
        $this->assign('ad',$ad);
        return view();
    }


    public function so99(){
        
        if(IS_AJAX()){

         $list=Db::table('so9_99product')->where(['state'=>3])->order('rand()')->limit(10)->select();
         return json($list);

        } 

        $list=Db::table('so9_99product')->where(['state'=>3])->select();
        $this->assign('list',$list);

        return view();


    }




    public function cate(){

      




        return view();


    }
    public function login(){

       


        if(IS_AJAX()){

         $s=input('param.');
         if($s['tel'] && $s['pwd']){
           
          $where['tel']=$s['tel'];
          $where['pwd']=md5($s['pwd']);

          $flag=Db::table('user')->where($where)->find();
          if($flag){


             $json['code']=200;

             
             session('uid',$flag['ID']);

          }else{

             $json['code']=400;
             $json['msg']="手机号或密码错误";



          }

          return json($json);
           
        


         }   


        }else{
        

          if(input('rurl')){
         
          $rurl=base64_decode(input('rurl'));

          }else{
         
          $rurl=u('weixin/index/index');

          }
         
         $this->assign('rurl',$rurl);   
         return view();

        }

        
       
    }


    public function reg(){
           

           if(IS_AJAX()){
            

            
            $s=input('param.');

          if(!$s['tel'] || !$s['pwd']){

            die;
          }


            $code=session($s['tel']);

           if($code!=$s['code']){
              
              $json['code']=400;
              $json['msg']="验证码错误";



           }else{




            $flag=Db::table('user')->where('tel',$s['tel'])->find();

            if($flag){

               $json['code']=400;
               $json['msg']="该手机号已经注册过搜禾网";


            }else{



            $data['tel']=$s['tel'];
            $data['pwd']=md5($s['pwd']);
            $data['addname']=$s['su'];
            $data['nickname']="搜禾用户".$s['code'];
            $data['wxid']=session('wxid');
            $data['pic']=session('wxpic');
            $data['addtime']=date('Y-m-d H:i:s');
            $uid=Db::table('user')->insertGetId($data);
            session('uid',$uid);
            $json['code']=200;

            }

           
           


           } 
          





           

            return json($json);


        }else{

       return view();

        }


        
    }

    public function rpwd(){


         if(IS_AJAX()){
            

            
            $s=input('param.');

          if(!$s['tel'] || !$s['pwd']){

            die;
          }


            $code=session($s['tel']);

           if($code!=$s['code']){
              
              $json['code']=400;
              $json['msg']="验证码错误";



           }else{




            $flag=Db::table('user')->where('tel',$s['tel'])->find();

            if(!$flag){

               $json['code']=400;
               $json['msg']="该手机号未注册搜禾网";


            }else{



           
            $data['pwd']=md5($s['pwd']);
            Db::table('user')->where('tel',$s['tel'])->update($data);
            $json['code']=200;

            }

           
           


           } 
          





           

            return json($json);


        }else{

       return view();

        }



    }


    public function apiteam(){

        if(IS_AJAX()){

            $s=input('param.');
            if($s['name'] && $s['tel']){


                $s['addtime']=date('Y-m-d H:i:s');
                $s['state']=1;
                $flag=Db::table('team_order')->insert($s);
                $json['code']= $flag ? 200 : 400;
                return json($json);


            }









        }else{


            return view();
        }




    }

    public function rz(){

        
        return view();
    }

    public function xieyi(){

        return view();
    }


   
   public function getcode(){

      $s=input('param.');
      if($s['tel']){

        $rand=mt_rand(1111,9999);
        $code=juhesms($s['tel'],$rand);
            if($code==200){

                 session($s['tel'],$rand);
                 $json['code']=200;

            }else{

               $json['code']=400;
               $json['msg']=$code;

            }
       
        
        
        return json($json);
      

      }




   }

   public function wxlogin(){

       if(!session('wxid')){
           
           getwxinfo();

       }else{


        $wxid=session('wxid');

        $user=Db::table('user')->where('wxid',$wxid)->find();

        if($user){

          session('uid',$user['ID']);
          $this->redirect('weixin/my/index');



        }else{


         $this->redirect('weixin/index/reg');


        }
           


       }
        
        

       


   }

   public function out(){

        session(null);
        $this->redirect('weixin/index/login');


   }



}
